const { semver, execa } = require("@vue/cli-shared-utils")
const { executeCommand } = require("./util/executeCommand")

const PACKAGE_MANAGER_CONFIG = {
 npm: {
  install: ["install", "--loglevel", "error"]
 }
}

class packageManager {
 constructor ({ context } = {}) {
  this.context = context || process.cwd()
  this.bin = "npm"
  this._registries = {}

  // npm版本处理
  const MIN_SUPPORTED_NPM_VERSION = "6.9.0"
  const npmVersion = execa.sync("npm", ["--version"]).stdout
 
  if (semver.lt(npmVersion, MIN_SUPPORTED_NPM_VERSION)) {
    throw new Error("NPM版本太低啦，请升级")
  }

  if (semver.gte(npmVersion, '7.0.0')){
   this.needsPeerDepsFix = true
  }
 
 }

 async install () {
  const args = []
  
  // npm版本大于7
  if (this.needsPeerDepsFix) {
    args.push("--legacy-peer-deps")
  }

  return await this.runCommand("install", args)
 }

  async runCommand (command, args) {
   await executeCommand(
    this.bin,
    [
     ...PACKAGE_MANAGER_CONFIG[this.bin][command],
     ...(args || [])
    ],
    this.context
   )
  }

}

module.exports = packageManager